﻿using System;
using System.Data;
using System.Data.SqlClient;
using System.Web;
using System.Web.UI;
using System.Collections;
using System.Web.UI.WebControls;
using AnyOffice.Components;
using AnyOffice.DAL;
using AnyOffice.Model;
using System.Text;
using log4net;
using System.Reflection;
using Microsoft.ApplicationBlocks.Data;

namespace AnyOffice.general.system.log
{
    public partial class manage : ModulePage
    {
        private static ILog log = LogManager.GetLogger(MethodBase.GetCurrentMethod().DeclaringType);

       
        protected void Page_Load(object sender, EventArgs e)
        {
            if (!this.Page.IsPostBack)
            {
                string commandText = "SELECT [CodeNo],[CodeName] FROM [dbo].[Sys_Code] WHERE [CodeFlag] = 'SYS_LOG'";
                DataTable table = SqlHelper.ExecuteDataset(base.Conn, CommandType.Text , commandText).Tables[0];
                this.ddlLogType.DataSource = table;
                this.ddlLogType.DataValueField = "CodeNo";
                this.ddlLogType.DataTextField = "CodeName";
                this.ddlLogType.DataBind();
                this.ddlLogType.Items.Insert(0, new ListItem("所有日志", "0"));
                this.txtEndTime.Value = DateTime.Now.ToString();
            }
            if (this.Page.IsPostBack)
            {
                this.pnlMain.Visible = false;
                this.litNavTitle.Text = "查询结果 （最多显示500条记录）";
                StringBuilder builder = new StringBuilder();
                if (this.rblAction.SelectedValue.ToLower() == "query")
                {
                    builder.Append("SELECT TOP 500 ");
                    builder.Append("\t[Sys_Log].[LogId],[Sys_Log].[UserId],[Sys_Log].[LogTime],[Sys_Log].[Ip],[Sys_Log].[Remark],[Users].[UserName],[Sys_Code].[CodeName] ");
                    builder.Append("FROM ");
                    builder.Append("\t[dbo].[Sys_Log] ");
                    builder.Append("\tLEFT OUTER JOIN [dbo].[Users] ON [Sys_Log].[UserId] = [Users].[UserId] ");
                    builder.Append("\tLEFT OUTER JOIN [dbo].[Sys_Code] ON CAST([Sys_Log].[Type] AS nvarchar(20)) = [Sys_Code].[CodeNo] ");
                    builder.Append("WHERE ");
                    builder.Append(" [Sys_Code].[CodeFlag] = 'SYS_LOG' ");
                    if (this.ddlLogType.SelectedValue != "0")
                    {
                        builder.Append("\tAND [Sys_Code].[CodeNo] = @Type ");
                    }
                    if (this.ToId.Value != string.Empty)
                    {
                        builder.Append("\tAND CHARINDEX(',' + [Users].[UserId] + ',',','+ @ToId + ',') > 0 ");
                    }
                    if (this.txtBeginTime.Value != string.Empty)
                    {
                        builder.Append("\tAND [Sys_Log].[LogTime] >= @BeginTime ");
                    }
                    if (this.txtEndTime.Value != string.Empty)
                    {
                        builder.Append("\tAND [Sys_Log].[LogTime] <= @EndTime ");
                    }
                    if (this.txtIp.Value != string.Empty)
                    {
                        builder.Append("\tAND [Sys_Log].[Ip] LIKE @Ip + '%' ");
                    }
                    builder.Append("ORDER BY ");
                    builder.Append("\t[LogId] DESC");
                }
                else
                {
                    builder.Append("DELETE ");
                    builder.Append("FROM ");
                    builder.Append("\t[dbo].[Sys_Log] ");
                    builder.Append("WHERE ");
                    builder.Append(" 1 = 1 ");
                    if (this.ddlLogType.SelectedValue != "0")
                    {
                        builder.Append("\tAND CAST([Type] AS nvarchar(20)) = @Type ");
                    }
                    if (this.ToId.Value != string.Empty)
                    {
                        builder.Append("\tAND CHARINDEX(',' + [UserId] + ',',','+ @ToId + ',') > 0 ");
                    }
                    if (this.txtBeginTime.Value != string.Empty)
                    {
                        builder.Append("\tAND [LogTime] >= @BeginTime ");
                    }
                    if (this.txtEndTime.Value != string.Empty)
                    {
                        builder.Append("\tAND [LogTime] <= @EndTime ");
                    }
                    if (this.txtIp.Value != string.Empty)
                    {
                        builder.Append("\tAND [Ip] LIKE @Ip + '%' ");
                    }
                }
                ArrayList list = new ArrayList();
                if (this.ddlLogType.SelectedValue != "0")
                {
                    list.Add(new object[] { "Type", this.ddlLogType.SelectedValue });
                }
                if (this.ToId.Value != string.Empty)
                {
                    list.Add(new object[] { "ToId", this.ToId.Value });
                }
                if (this.txtBeginTime.Value != string.Empty)
                {
                    string str2 = (this.txtBeginTime.Value.Length > 10) ? this.txtBeginTime.Value : (this.txtBeginTime.Value + " 00:00:00");
                    list.Add(new object[] { "BeginTime", Convert.ToDateTime(str2) });
                }
                if (this.txtEndTime.Value != string.Empty)
                {
                    string str3 = (this.txtEndTime.Value.Length > 10) ? this.txtEndTime.Value : (this.txtEndTime.Value + " 23:59:59");
                    list.Add(new object[] { "EndTime", Convert.ToDateTime(str3) });
                }
                if (this.txtIp.Value != string.Empty)
                {
                    list.Add(new object[] { "Ip", this.txtIp.Value });
                }
                SqlParameter[] commandParameters = new SqlParameter[list.Count];
                for (int i = 0; i < list.Count; i++)
                {
                    object[] objArray6 = (object[])list[i];
                    commandParameters[i] = new SqlParameter("@" + objArray6[0].ToString(), objArray6[1]);
                }
                if (this.rblAction.SelectedValue.ToLower() == "query")
                {
                    DataTable table2 = SqlHelper.ExecuteDataset(base.Conn, CommandType.Text , builder.ToString(), commandParameters).Tables[0];
                    if (table2.Rows.Count > 0)
                    {
                        this.pnlSearch.Visible = true;
                        this.rptLog.DataSource = table2;
                        this.rptLog.DataBind();
                    }
                    else
                    {
                        this.pnlSearch.Visible = false;
                        this.litMessage.Text = base.ShowPrompt("无符合条件的日志记录", "manage.aspx");
                    }
                }
                else
                {
                    SqlHelper.ExecuteNonQuery(base.Conn, CommandType.Text, builder.ToString(), commandParameters);
                    base.CloseConn();
                    Response.Redirect("manage.aspx");
                }
            }
        } 

    }
}
